package com.my.demo.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

@Slf4j
@Component
public class WechatMessageUtils {

    private static MessageSource messageSource;


    public WechatMessageUtils(MessageSource messageSource) {
        if (log.isInfoEnabled()) {
            log.info("MessageUtils construction success");
        }
        WechatMessageUtils.messageSource = messageSource;
    }

    /**
     * 获取单个国际化翻译值
     */
    public static String get(String msgKey) {
        Locale locale = LocaleContextHolder.getLocale();
        try {
            return messageSource.getMessage(msgKey, null, locale);
        } catch (Exception e) {
            log.error("msgKey:{},locale:{},MessageUtils.get Exception:{}", msgKey, locale, e.getMessage());
            return messageSource.getMessage(msgKey, null, Locale.US);
        }
    }
}
